package com.gtp.kafka.demo;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class KafkaService {

	@Autowired
	private KafkaTemplate<String,String> kafkaTemplate;

	//发消息
	public void toSend() {
		// topic, key, data
		kafkaTemplate.send("test", "test", "test-ok");
	}

	//消费消息:KafkaHandler
	@KafkaListener(topics = { "test" },groupId="groupId1")
	public void listen(ConsumerRecord<String,String> record) {
		System.out.println(record);
	}

}
